/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.FinanceDAO;
import com.core.database.Manager;
import com.core.entities.Finance;
import com.core.exception.DAOException;

/**
 * 
 * @author himanasser
 */
public class FinanceDAOImpl implements FinanceDAO {
		Finance finance = new Finance();
		ArrayList<Finance> financesList = new ArrayList<Finance>();


	Connection connection = null;
	private static FinanceDAOImpl instance=null;
	public static FinanceDAOImpl getInstance(){
		if(instance==null){
			instance=new FinanceDAOImpl();
		}
		return instance;
	}

	@Override
	public Finance findByCost(String cost) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM finance WHERE cost = '" + cost + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					finance.setId(rs.getInt(1));
					finance.setCost(rs.getString(2));
					finance.setDescription(rs.getString(3));
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return finance;
	}

	@Override
	public Finance findByDescription(String description) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT *FROM finance WHERE description = '"
					+ description + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					finance.setId(rs.getInt(1));
					finance.setCost(rs.getString(2));
					finance.setDescription(rs.getString(3));
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return finance;
	}

	@Override
	public void insert(Finance o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "INSERT INTO finance (cost, description) VALUE (?, ? )";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, o.getCost());
			ps.setString(2, o.getDescription());

			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException("no row inserted!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("Cannot add new Finance", ex);
		} finally {
			Manager.closeConnection(connection);
		}
	}

	@Override
	public Finance findById(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM finance WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					finance.setId(rs.getInt(1));
					finance.setCost(rs.getString(2));
					finance.setDescription(rs.getString(3));
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return finance;
	}

	@Override
	public List<Finance> findAll() throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " SELECT * FROM finance ";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				finance.setId(rs.getInt(1));
				finance.setCost(rs.getString(2));
				finance.setDescription(rs.getString(3));
				financesList.add(finance);
			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return financesList;
	}

	@Override
	public void delete(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM finance WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setString(1, finance.getCost());
				ps.setString(2, finance.getDescription());
			}

		} catch (SQLException s) {
			s.printStackTrace();

		}
	}

	@Override
	public void update(Finance o) throws DAOException {

		try {
			connection = Manager.getConnection();
			String sql = "UPDATE finance SET cost = '" + o.getCost()
					+ " ', description = '" + o.getDescription()
					+ "' WHERE id = " + o.getId();
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setString(1, o.getCost());
				ps.setString(2, o.getDescription());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
	}
}
